const WebSocket = require('ws');  // 使用 require 而不是 import
const wss = new WebSocket.Server({ port: 3000 });

wss.on('connection', (ws) => {
    ws.on('message', (message) => {
        // 广播消息给所有连接的客户端
        wss.clients.forEach(client => {
            if (client !== ws && client.readyState === WebSocket.OPEN) {
                client.send(message);
            }
        });
    });

    ws.send(JSON.stringify({ message: 'Welcome to the signaling server' }));
});

console.log('Signaling server is running on ws://localhost:3000');
// import { Server } from 'socket.io';

// const io = new Server(3000, {
//     cors: {
//         origin: '*',
//     }
// });

// io.on('connection', socket => {
//     console.log('A user connected');

//     socket.on('offer', offer => {
//         socket.broadcast.emit('offer', offer);
//     });

//     socket.on('answer', answer => {
//         socket.broadcast.emit('answer', answer);
//     });

//     socket.on('ice-candidate', candidate => {
//         socket.broadcast.emit('ice-candidate', candidate);
//     });

//     socket.on('disconnect', () => {
//         console.log('A user disconnected');
//     });
// });